$(".btnName").on('click', function () {
    var param = {startIndex: 0, pageSize: 3, name: $('#queryName').val().trim(), sex: $("#querySex").val()};//索引 0   每页条数 2
    console.info(param.name);
    console.info(param.sex);
    if (param.sex == '男') {
        param.sex = 1;
    } else if (param.sex == '女') {
        param.sex = 0;
    } else {
        param.sex = -1;
    }
    var $wrapper = $('#div-table-container');
    var $table = $('#table-list');
    $table.dataTable().fnDestroy();
    // param.name=$("#queryName").val().trim();
    var ajaxUrl = "/student/tea/getPage";
    var _table = $table.dataTable($.extend(true, {}, CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
        ajax: function (data, callback, settings) {
            // $wrapper.spinModal();
            param.startIndex = data.start;
            // param.name="林";
            $.ajax({
                type: "post",
                url: ajaxUrl,
                cache: false,
                data: param,        //传入请求参数
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    setTimeout(function () {
                        var returnData = {};
                        returnData.draw = data.draw + 1;
                        returnData.recordsTotal = result.total;
                        returnData.recordsFiltered = result.total;//每次翻页过滤数据条数
                        returnData.data = result.pageData;
                        // $wrapper.spinModal(false);
                        callback(returnData);
                    }, 200);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("查询失败");
                    // $wrapper.spinModal(false);
                }
            });
        },
        ordering: false,
        pageLength: param.pageSize,// 每页条数 设置
        columns: [
            CONSTANT.DATA_TABLES.COLUMN.CHECKBOX,
            {
                className: "ellipsis",
                data: "id",
                width: "40px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,
            },
            {
                className: "ellipsis",
                data: "teacherNum",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS
            },
            {
                className: "ellipsis",
                data: "teacherName",
                width: "100px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS

            },

            {
                className: "ellipsis",
                data: "teacherSex",
                width: "60px",
                render: function (data, type, row, meta) {
                    return data == 1 ? "男" : "女";
                }
            },
            {
                className: "ellipsis",
                data: "post",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS
            },
            {
                className: "td-operation",
                data: null,
                defaultContent: "",
                width: "200px"
            }
        ],
        "createdRow": function (row, data, index) {
            if (data.role) {
                $(row).addClass("info");
            }
            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-detail">详细</button>');
            $('td', row).eq(6).append($btnDetail);

            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-edit">修改</button>');
            $('td', row).eq(6).append($btnDetail);

            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-delete">删除</button>');
            $('td', row).eq(6).append($btnDetail);
        },
        "drawCallback": function (settings) {
            $(":checkbox[name='cb-check-all']", $wrapper).prop('checked', false);
            $("tbody tr", $table).eq(0).click();
        }
    })).api();

    $table.on("change", ":checkbox", function () {
        if ($(this).is("[name='cb-check-all']")) {
            // 全选
            $(":checkbox", $table).prop("checked", $(this).prop("checked"));
        } else {
            // 一般复选
            var checkbox = $("tbody :checkbox", $table);
            $(":checkbox[name='cb-check-all']", $table).prop('checked', checkbox.length == checkbox.filter(':checked').length);
        }
    }).on("click", ".td-checkbox", function (event) {
        // 点击单元格即点击复选框
        !$(event.target).is(":checkbox") && $(":checkbox", this).trigger("click");
    }).on("click", ".btn-edit", function () {
        // 点击修改按钮
        var item = _table.row($(this).closest('tr')).data();
        $('#id-edit').val(item.id);
        $('#teacherNum-edit').val(item.teacherNum);
        $('#teacherName-edit').val(item.teacherName);
        $('#teacherSex-edit').val(item.teacherSex);
        $('#post-edit').val(item.post);
        $('#myModal-edit').modal('show');
    }).on("click", ".btn-delete", function () {
        // 点击删除按钮
        var item = _table.row($(this).closest('tr')).data();
        var flag = confirm("确定删除教师记录吗？");
        console.info(flag);
        var objP = {'id': item.id};
        if (flag) {
            $.ajax({
                type: "post",
                url: "/student/tea/deleteTeacher",
                cache: false,
                data: objP,
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    console.info(result);
                    if (result.success) {
                        _table.draw(false);
                    } else {
                        alert(result.message);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("保存失败");
                }
            });
        }
    });


    $('#add-teacher-submit').on('click', function () {//绑定点击事件

        var objP = {};
        objP.teacherNum = $('#teacherNum-add').val().trim();
        objP.teacherName = $('#teacherName-add').val().trim();
        objP.teacherSex = $('#teacherSex-add').val().trim();
        objP.post = $('#post-add').val();

        if (objP.teacherNum == "") {
            alert("教师工号不能为空");
            return;
        }

        if (objP.teacherName == "") {
            alert("教师姓名不能为空");
            return;
        }

        $.ajax({
            type: "post",
            url: "/student/tea/addTeacher",
            cache: false,
            data: objP,
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            dataType: "json",
            success: function (result) {
                console.info(result);
                if (result.success) {
                    _table.draw(false);
                    $('#myModal-add').modal('hide');
                    document.getElementById('add-teacher-form').reset();
                } else {
                    alert(result.message);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("保存失败");
            }
        });

    });


    $('#edit-teacher-submit').on('click', function () {//绑定点击事件

        var objP = {};
        objP.id = $('#id-edit').val();
        objP.teacherNum = $('#teacherNum-edit').val().trim();
        objP.teacherName = $('#teacherName-edit').val().trim();
        objP.teacherSex = $('#teacherSex-edit').val().trim();
        objP.post = $('#post-edit').val();

        if (objP.id == "" || objP.id < 0) {
            alert("无法修改，记录主键丢失");
            return;
        }

        if (objP.teacherNum == "") {
            alert("教师工号不能为空");
            return;
        }

        if (objP.teacherName == "") {
            alert("教师姓名不能为空");
            return;
        }

        $.ajax({
            type: "post",
            url: "/student/tea/updateTeacher",
            cache: false,
            data: objP,
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            dataType: "json",
            success: function (result) {
                console.info(result);
                if (result.success) {
                    _table.draw(false);
                    $('#myModal-edit').modal('hide');
                    document.getElementById('edit-teacher-form').reset();
                } else {
                    alert(result.message);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("保存失败");
            }
        });

    });

});
